package com.jrx.anytxn.param.service.aviator.impl;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.aviator.AviatorRuleSetTypeReq;
import com.jrx.anytxn.param.dto.aviator.AviatorRuleSetTypeRes;
import com.jrx.anytxn.param.entity.RmAviatorRuleSetType;
import com.jrx.anytxn.param.mapper.ext.ExtRmAviatorRuleSetTypeMapper;
import com.jrx.anytxn.param.service.aviator.IAviatorRuleSetTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * 规则集接口实现
 * @author Qingyao Xing
 * @date 2019/10/10
 */
@Service
public class AviatorRuleSetTypeServiceImpl implements IAviatorRuleSetTypeService {

    private static final Logger logger= LoggerFactory.getLogger(AviatorRuleSetTypeServiceImpl.class);
    @Resource
    private ExtRmAviatorRuleSetTypeMapper extRmAviatorRuleSetTypeMapper;

    @Autowired
    private ISegmentService segmentService;



    @Override
    public boolean saveAviatorRuleSetType(AviatorRuleSetTypeReq aviatorRuleSetTypeReq) throws TxnBizException {
        RmAviatorRuleSetType copy = BeanMapping.copy(aviatorRuleSetTypeReq, RmAviatorRuleSetType.class);
        try {
            copy.setId(segmentService.getId("RmAviatorRuleSetType"));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        copy.setRecordVersionNumber(Constant.VERSION_NUMBER);
        int i = extRmAviatorRuleSetTypeMapper.insertSelective(copy);
        return i > 0;
    }

    @Override
    public boolean modifyAviatorRuleSetType(AviatorRuleSetTypeReq aviatorRuleSetTypeReq) {
        RmAviatorRuleSetType copy = BeanMapping.copy(aviatorRuleSetTypeReq, RmAviatorRuleSetType.class);
        int i = extRmAviatorRuleSetTypeMapper.updateByIdAndTenId(copy);
        return i > 0;
    }

    @Override
    public List<AviatorRuleSetTypeRes> getAviatorRuleSetType(String proFlag,String tenantId) throws TxnException {
        List<RmAviatorRuleSetType> list = extRmAviatorRuleSetTypeMapper.selectByProFlg(proFlag,tenantId);
        if (list.isEmpty()) {
            throw new TxnException("规则集为空");
        }
        return BeanMapping.copyList(list, AviatorRuleSetTypeRes.class);
    }

    @Override
    public boolean removeAviatorRuleSetType(Long id,String tenantId) {
        int i = extRmAviatorRuleSetTypeMapper.deleteByIdAndTenId(id,tenantId);
        return i > 0;
    }
}
